<?php

class Duclinh_model extends Model {
	
	private $pd = 'products';
	private $mn = 'menus';
	private $op = 'options';
	private $ct = 'categorys';
	private $mp = 'mpage';
	private $ns = 'news';
	private $pf = 'products_field';
	private $ae = 'advertise';
	
	function Duclinh_model() {
		parent::Model ();
	}
		
	function get_menu( $lg = 1, $vl = 1 ) {
		$data = array();
		
		$query = $this->db->query("SELECT `id`, `name`, `description`, `style`, `link` FROM `$this->mn` WHERE `id_lang` = '$lg' AND `status` = '1' AND `place` = '$vl' ORDER BY `orders` ASC, `id` DESC");
		
		if ( $query->num_rows () > 0 ) 
			$data = $query->result_array();
				
		$query->free_result ();		
		return $data;
	}
	
	function get_cat( $lg = 1, $ty = 0, $cat = 0 ) {
		$data = array();
		
		if ( $cat == -1 )
			$sql = "SELECT `id`, `name` FROM `$this->ct` WHERE `type` = '$ty' AND `id_lang` = '$lg' AND `home` = 1 ORDER BY `orders` ASC, `id` DESC";
		else
			$sql = "SELECT `id`, `id_parent`, `type`, `lever`, `name`, `description`, `style`, `link` FROM `$this->ct` WHERE `type` = '$ty' AND `id_lang` = '$lg' AND `id_parent` = '$cat' AND `status` = 1 ORDER BY `orders` ASC, `id` DESC";
		
		$query = $this->db->query($sql);
		
		if ( $query->num_rows () > 0 ) 
			$data = $query->result_array();
		
		$query->free_result ();
		return $data;
	}
	
	function get_setting( $lg = 1 ) {
		$data = array ();		
		$query = $this->db->query("SELECT `name`, `slogan`, `description`, `keywords`, `robots`, `googlebot`, `abstract`, `copyright`, `copyright2` FROM `$this->op` WHERE `id_lang` = '$lg' LIMIT 1 ");
		
		if ( $query->num_rows () > 0 ) 
			$data = $query->row_array();
		
		$query->free_result ();		
		return $data;
	}	
	
	function get_product_status( $lg = 1, $vl = 0, $l = 0, $cat = 0 ) {
		$data = array();

		switch ( $vl ) {
			case 0: // Sản phẩm mới
					$where = "AND `new` = '1' ";
				break;
				
			case 1: // Sản phẩm tiêu biểu
					$where = "AND `focus` = '1' ";
				break;

			case 2: // Trang chủ
					$where = "AND `home` = '1' AND `cat_id` = '$cat' ";
				break;
		}
		
		if ( $l != 0 ) 
			$limit = " LIMIT $l ";
		else 
			$limit = '';
		
		$conf = $this->config->item('myn_product_order');
		
		switch ( $conf ) {
			case 0:
					$order = '`id` DESC';
				break;
				
			case 1:
					$order = '`vnd` ASC';
				break;

			case 2:
					$order = '`vnd` DESC';
				break;			

			case 3:
					$order = '`orders` DESC';
				break;
		}
		
			
		$query = $this->db->query("SELECT `id`, `cat_id`, `name`, `description`, `image`, `image_text`, `usd`, `vnd`, `field_1`, `field_2`, `field_3`, `field_4`, `field_5` FROM `$this->pd` WHERE `id_lang` = '$lg' AND `status` = '1' $where ORDER BY $order $limit");
		
		if ( $query->num_rows () > 0 ) 
			$data = $query->result_array();
				
		$query->free_result ();		
		return $data;
	}
	
	function get_news_new( $lg = 1, $ty = 0, $l = 0 ) {
		$data = array();

		if ( $l != 0 ) 
			$limit = " LIMIT $l ";
		else 
			$limit = '';
		
		$query = $this->db->query("SELECT `id`, `cat_id`, `name`, `description`, `image`, `image_text` FROM `$this->ns` WHERE `id_lang` = '$lg' AND `status` = '1' AND `type` = '$ty' AND `new` = 1 ORDER BY `orders` ASC, `id` DESC $limit");
		
		if ( $query->num_rows () > 0 ) 
			$data = $query->result_array();
				
		$query->free_result ();		
		return $data;
	}
	
	function get_detail( $lg = 1, $id = 0, $ty = 'product' ) {
		$data = array ();	

		if ( is_numeric( $id ) ) {

			switch ( $ty ) {
				case 'product':
						$sql = "SELECT a.`vnd`, a.`attachment` ,a.`technical`, a.`seo_description`, a.`seo_keywords`, a.`id`, a.`cat_id`, a.`name`, a.`detail`, a.`image`, a.`image_text`, a.`field_1`, a.`field_2` , a.`field_3` , a.`field_4` , a.`field_5` , a.`field_6` , a.`field_7` , a.`field_8` , a.`field_9`, a.`field_10`, a.`field_11`, a.`field_12`, a.`field_13`, a.`field_14`, a.`field_15`,  
						b.`field_name_1`, b.`field_name_2`, b.`field_name_3`, b.`field_name_4`, b.`field_name_5`, b.`field_name_6`, b.`field_name_7`, b.`field_name_8`, b.`field_name_9`, b.`field_name_10`, b.`field_name_11`, b.`field_name_12`, b.`field_name_13`, b.`field_name_14`, b.`field_name_15`
						FROM `$this->pd` AS a 
						LEFT JOIN `$this->pf` AS b ON a.`id_lang` = b.`id_lang`
						WHERE a.`id_lang` = '$lg' AND a.`id` = '$id' LIMIT 1 ";							
					break;
				
				case 'cart':
						$sql = "SELECT 
									`vnd`, `id`, `name`						
								FROM 
									`$this->pd`
								WHERE 
									`id_lang` = '$lg' AND `id` = '$id' 
								LIMIT 1 ";							
					break;
						
				case 'page':
						$sql = "SELECT `name`, `description` 
						FROM `$this->mp` 
						WHERE `id_lang` = '$lg' AND `id` = '$id' 
						LIMIT 1 ";	
					break;

				case 'news':
						$sql = "SELECT `id`, `cat_id`, `name`, `detail` 
						FROM `$this->ns` 
						WHERE `id_lang` = '$lg' AND `id` = '$id' AND `type` = '0'
						LIMIT 1 ";	
					break;
					
				case 'service':
						$sql = "SELECT `id`, `cat_id`, `name`, `detail` 
						FROM `$this->ns` 
						WHERE `id_lang` = '$lg' AND `id` = '$id' AND `type` = '1'
						LIMIT 1 ";	
					break;

				case 'download':
						$sql = "SELECT `id`, `cat_id`, `name`, `detail`, `file_attach` 
						FROM `$this->ns` 
						WHERE `id_lang` = '$lg' AND `id` = '$id' AND `type` = '4'
						LIMIT 1 ";	
					break;
							
				case 'catname':
						$sql = "SELECT `name` FROM `$this->ct` WHERE `id_lang` = '$lg' AND `id` = '$id' LIMIT 1 ";
					break;
					
			}
			
			$query = $this->db->query( $sql );
			
			if ( $query->num_rows () > 0 ) 
				$data = $query->row_array();
			
			$query->free_result ();	
		}	
		return $data;
	}
	
	function get_other( $lg = 1, $id = 0, $cat = 0, $ty = 'product' ) {
		$data = array ();	

		if ( is_numeric( $id ) ) {

			switch ( $ty ) {
				case 'product':
						$sql = "SELECT `id`, `name`, `image`, `image_text` FROM `$this->pd` WHERE `id_lang` = '$lg' AND `cat_id` = '$cat' AND `id` != '$id' ORDER BY `id` DESC LIMIT 10";					
					break;

				case 'news':
						$sql = "SELECT `id`, `name` FROM `$this->ns` WHERE `id_lang` = '$lg' AND `cat_id` = '$cat' AND `id` != '$id' ORDER BY `id` DESC LIMIT 10";	
					break;
					
				case 'service':
						$sql = "SELECT `id`, `name` FROM `$this->ns` WHERE `id_lang` = '$lg' AND `cat_id` = '$cat' AND `id` != '$id' ORDER BY `id` DESC LIMIT 10";	
					break;
					
				case 'download':
						$sql = "SELECT `id`, `name` FROM `$this->ns` WHERE `id_lang` = '$lg' AND `cat_id` = '$cat' AND `id` != '$id' ORDER BY `id` DESC LIMIT 10";	
					break;	
			}
			
			$query = $this->db->query( $sql );
			
			if ( $query->num_rows () > 0 ) 
				$data = $query->result_array();
			
			$query->free_result ();	
		}	
		return $data;
	}
	
	function get_items ( $lg = 1, $vl = 0, $ty = 'product', $num = 0 , $offset, &$total ) {
		$data = array();
		
		switch ( $ty ) {
			case 'product':
				
					$conf = $this->config->item('myn_product_order');
					switch ( $conf ) {
						case 0:
								$order = '`id` DESC';
							break;
							
						case 1:
								$order = '`vnd` ASC';
							break;
			
						case 2:
								$order = '`vnd` DESC';
							break;		

						case 3:
								$order = '`orders` DESC';
							break;
					}
					
					$sql = "SELECT `id`, `cat_id`, `name`, `description`, `image`, `image_text`, `usd`, `vnd`, `field_1`, `field_2`, `field_3`, `field_4`, `field_5`
					FROM `$this->pd` 
					WHERE `id_lang` = '$lg' AND `status` = '1' AND `cat_id` = '$vl' 
					ORDER BY $order LIMIT $offset, $num";
					
					$sqs = "SELECT `id` FROM `$this->pd` WHERE `id_lang` = '$lg' AND `status` = '1' AND `cat_id` = '$vl' ";
				break;
			
			case 'search':
				
					$conf = $this->config->item('myn_product_order');
					
					switch ( $conf ) {
						case 0:
								$order = '`id` DESC';
							break;
							
						case 1:
								$order = '`vnd` ASC';
							break;
			
						case 2:
								$order = '`vnd` DESC';
							break;		

						case 3:
								$order = '`orders` DESC';
							break;
					}
					
					$sql = "SELECT `id`, `cat_id`, `name`, `description`, `image`, `image_text`, `usd`, `vnd`, `field_1`, `field_2`, `field_3`, `field_4`, `field_5`
					FROM `$this->pd` 
					WHERE `id_lang` = '$lg' AND `status` = '1' AND `name` LIKE '%$vl%' 
					ORDER BY $order LIMIT $offset, $num";
					
					$sqs = "SELECT `id` FROM `$this->pd` WHERE `id_lang` = '$lg' AND `status` = '1' AND `name` LIKE '%$vl%'";
				break;
					
			case 'service':
					$sql = "SELECT `id`, `cat_id`, `name`, `description`, `image`, `image_text`
					FROM `$this->ns` 
					WHERE `id_lang` = '$lg' AND `status` = '1' AND `cat_id` = '$vl' AND `type` = '1' 
					ORDER BY `orders` ASC, `id` DESC LIMIT $offset, $num";
					
					$sqs = "SELECT `id` FROM `$this->ns` WHERE `id_lang` = '$lg' AND `status` = '1' AND `cat_id` = '$vl' AND `type` = '1' ";
				break;

			case 'news':
					$sql = "SELECT `id`, `cat_id`, `name`, `description`, `image`, `image_text`
					FROM `$this->ns` 
					WHERE `id_lang` = '$lg' AND `status` = '1' AND `cat_id` = '$vl' AND `type` = '0'
					ORDER BY `orders` ASC, `id` DESC LIMIT $offset, $num";
					
					$sqs = "SELECT `id` FROM `$this->ns` WHERE `id_lang` = '$lg' AND `status` = '1' AND `cat_id` = '$vl' AND `type` = '0' ";
				break;	
				
			case 'download':
				
					if ( $vl == 0 ) 
						$tam = '';
					else
						$tam = " AND `cat_id` = '$vl'";
						
					$sql = "SELECT `id`, `cat_id`, `name`, `description`, `image`, `image_text`
					FROM `$this->ns` 
					WHERE `id_lang` = '$lg' AND `status` = '1' $tam AND `type` = '4'
					ORDER BY `orders` ASC, `id` DESC LIMIT $offset, $num";
					
					$sqs = "SELECT `id` FROM `$this->ns` WHERE `id_lang` = '$lg' AND `status` = '1' $tam AND `type` = '4' ";
				break;
		}
				
		$query = $this->db->query( $sql );
		
		if ( $query->num_rows () > 0 ) 
			$data = $query->result_array();

		// Tông số bản ghi
		$query = $this->db->query($sqs);
		$total = $query->num_rows();
		
		$query->free_result ();		
		return $data;
	}

	function get_list_adv( $place = 0 ) {
		$data = array ();
				
		$query = $this->db->query("SELECT `image`, `image_type`, `image_width`, `image_height`, `link` FROM `$this->ae` WHERE `place` = '$place' AND `status` = '1' ORDER BY `orders` ASC, `id` DESC");
		
		if ( $query->num_rows () > 0 ) 
			$data = $query->result_array();		
		
		$query->free_result ();
		
		return $data;
	}
	
	function search_item ( $lg = 1, $vl = 0, $catid = 0 ,$num = 0 , $offset, &$total ) {
		$data = array();
		
		$conf = $this->config->item('myn_product_order');
		
		switch ( $conf ) {
			case 0:
					$order = '`id` DESC';
				break;
				
			case 1:
					$order = '`vnd` ASC';
				break;

			case 2:
					$order = '`vnd` DESC';
				break;		

			case 3:
					$order = '`orders` DESC';
				break;
		}
		
		if ( $catid == 0 )
			$where = '';
		else
			$where = " AND `cat_id` = $catid";
		
		$sql = "SELECT `id`, `cat_id`, `name`, `description`, `image`, `image_text`, `usd`, `vnd`, `field_1`, `field_2`, `field_3`, `field_4`, `field_5`
				FROM `$this->pd` 
				WHERE `id_lang` = '$lg' AND `status` = '1' AND `name` LIKE '%$vl%' $where
				ORDER BY $order LIMIT $offset, $num";
						
		$query = $this->db->query( $sql );
		
		if ( $query->num_rows () > 0 ) 
			$data = $query->result_array();

		// Tông số bản ghi
		$query = $this->db->query( "SELECT `id` FROM `$this->pd` WHERE `id_lang` = '$lg' AND `status` = '1' AND `name` LIKE '%$vl%' $where " );
		$total = $query->num_rows();
		
		$query->free_result ();		
		return $data;
	}
}
?>